var scanInput = document.getElementById("scanInput");
var cargoSpace = document.getElementById("cargoSpace");
var huoCode = "";
var newHou = "";
var code = "";
var typeStr = "";
var funName = "Phone_GetStore_YiKu_Sub";
var yikuData = null;

(function(e) {
	mui.plusReady(function() {
		mui.init({
			beforeback: function () {
				if($("#moveBefo").is(':hidden')) {
					$("#moveBefo").show();
					$(".out-sid").hide();
					return false;
				}
			}
		});
		typeStr = getQueryString("typeStr");
		$("#title").html(typeStr);

		if(typeStr == "移库操作") {
			$("#moveBefoBtn").html("移库");
		} else {
			$("#moveBefoBtn").html("确认合托");
			$('.list1 .item-header span').text('合出明细');
			$('#scanInput').attr('placeholder', '合出托号标签');
			$('#cargoSpace').attr('placeholder', '合入托号标签');
			funName = "Phone_GetStore_HeTuo";
		}

	});
})(mui);

$("#moveInput").bind('change', function() {
	newHou = $(this).val().trim();
});

document.onkeydown = function() {
	if(window.event.keyCode == 120 || window.event.keyCode == 13) {
		
		if(typeStr == "移库操作") {
			YiKuSub(funName);
		} else {
			YiKuSub(funName);
		}
	}
}

$('#scanInput, #cargoSpace').blur(function () {
	if(typeStr == "移库操作") {
		YiKuSub(funName);
	} else {
		YiKuSub(funName);
	}
});

function YiKuSub(funName) {
	huoCode = $('#cargoSpace').val().trim();
	code = $('#scanInput').val().trim();
	
	$('#infoList1, #infoList2').html('');
	yikuData = null;	// 初始化移库数据
	if(!huoCode || !code) {
		return;
	}
		
	var jsonData = {
		"op": funName,
		"HuoWei": huoCode,
		"Out": code,
		"In": huoCode,
		"Code": code,
		'Pid': userInfo.pid
	}

	var opts = {
		url: "",
		addressUrl: "testline",
		requestType: "POST",
		pastDate: jsonData
	};
	console.log(JSON.stringify(opts));
	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		console.log(JSON.stringify(data));
		plus.nativeUI.closeWaiting();
		if(!isEmpty(data)) {
			yikuData = data;
			if(typeStr == "移库操作") {
				$(".list1").show();
			} else {
				$(".list1, .list2").show();
			}
			for(var i = 0; i < data.length; i++) {
				createItem(data[i]);
			}
		} else {
			yikuData = [];
		}
	}, function(s) {

	});
}

$("#moveBefoBtn").click(function() {
	huoCode = $('#cargoSpace').val().trim();
	code = $('#scanInput').val().trim();
	if(typeStr == "移库操作") {
		if(isEmpty(code) || isEmpty(huoCode)) {
			mui.toast("请完善移库信息");
			return;
		}
		yikuCheck();
	} else {
		hetuo();
	}

});

function yikuCheck() {
	if(yikuData === null) {
		setTimeout(function () {
			yikuCheck();
		}, 100);
	} else if (yikuData.length == 0) {
		mui.toast('无可移库数据');
	} else{
		$("#moveBefo").hide();
		$(".out-sid").show();
	}
}

$("#moveAfterBtn").click(function() {
	if(isEmpty(newHou)) {
		mui.toast("请扫描或录入新货位码");
		return;
	}

	var jsonData = {
		"op": "Phone_SetStore_YiKu_Finish",
		"HuoWei": newHou,
		"RFID": code,
		"Pid": userInfo.Pid
	}

	var opts = {
		url: "",
		addressUrl: "testline",
		requestType: "POST",
		pastDate: jsonData
	};
	console.log(JSON.stringify(opts));
	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		console.log(JSON.stringify(data));
		plus.nativeUI.closeWaiting();
		$(".out-sid").hide();
		$("#moveBefo").show();
		mui.toast('操作成功');
		$('#cargoSpace, #scanInput').val('');
		$('#infoList1, #infoList2').html('');
	}, function(s) {

	});
});

function createItem(item) {
	var hetuoStr = '';
	if(typeStr == "合托操作") {
		hetuoStr = '<span>状态：</span><em>' + item.T_Status + '</em><br />';
	}
	var html = '<li class="item-info p-10" ><span>名称：</span><em>' + (item.SMName || item.SM_Name || '') + '</em><br />' +
		'<span>规格：</span><em>' + item.Spec + '</em><br />' +
		'<span>数量：</span><em><input type="number" disabled="disabled" id="article"  value="' + item.Num + '" />个</em><br />' +
		'<span>货位：</span><em>' + (item.SPPlace || item.HuoWei || '') + '</em><br />' +
		'<span>托号：</span><em>' + item.TuoCode + '</em><br />' + hetuoStr + '</li>';
		console.log(html);
		
	if(item.T_Status == '合入') {
		$("#infoList2").append(html);
	} else {
		$("#infoList1").append(html);
	}
};

function hetuo() {
	var jsonData = {
		"op": "Phone_SetStore_HeTuo",
		"Out": code,
		"In": huoCode,
		"Pid": userInfo.Pid
	}

	if(checkParams(jsonData)) {
		mui.toast("请输入或扫描条码");
		return;
	}
	var opts = {
		url: "",
		addressUrl: "testline",
		requestType: "POST",
		pastDate: jsonData
	};
	console.log(JSON.stringify(opts));
	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		console.log(JSON.stringify(data));
		$('#cargoSpace, #scanInput').val('');
		mui.toast('操作成功');
		$('#infoList1, #infoList2').html('');
	}, function(s) {
		
	});
}